package com.ytd.smartcanteen.weighting.dao;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.ytd.common.base.BaseApplication;
import com.ytd.common.util.LogUtil;
import com.ytd.smartcanteen.weighting.bean.MealCategoryInfo;
import com.ytd.smartcanteen.weighting.bean.MealInfo;
import com.ytd.smartcanteen.weighting.bean.MealTimeInfo;
import com.ytd.smartcanteen.weighting.bean.OrderBean;

/**
 * Your class notes
 *
 * @author gaoguanchao
 * @since 2023/3/24
 */
@Database(entities = {MealInfo.class, MealTimeInfo.class, MealCategoryInfo.class,
    OrderBean.class}, version = 7, exportSchema = false)
public abstract class WeightingDB extends RoomDatabase {
    private static volatile WeightingDB instance;

    public abstract MealCategoryInfoDao getMealCategoryInfoDao();

    public abstract MealTimeInfoDao getMealTimeInfoDao();

    public abstract MealInfoDao getMealInfoDao();

    public abstract OrderBeanDao getOrderBeanDao();

    private static boolean isPrintLog = false;

    public static WeightingDB getInstance() {

        if (instance == null) {
            synchronized (WeightingDB.class) {
                if (instance == null) {
                    Migration[] mMigrations =
                        new Migration[] {MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6,
                            MIGRATION_6_7};
                    instance =
                        Room.databaseBuilder(BaseApplication.getContext().getApplicationContext(), WeightingDB.class,
                                "weighting.db")
                            .fallbackToDestructiveMigration()
                            .allowMainThreadQueries()
                            .addMigrations(mMigrations)
                            .build();
                }
            }
        }
        if (instance.mDatabase != null && !isPrintLog) {
            isPrintLog = true;
            LogUtil.d("WeightingDB Version:" + instance.mDatabase.getVersion());
        }
        return instance;
    }

    private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //在这里做升级操作，比如增加或者减少表中的字段
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN numberUnit TEXT");
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN numberUnitName TEXT");
        }
    };

    private static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE orderbean ADD COLUMN uploadState INTEGER NOT NULL DEFAULT 0");
        }
    };

    private static final Migration MIGRATION_3_4 = new Migration(3, 4) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN whetherLimitWeightOfTake INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN limitWeightOfTake INTEGER NOT NULL DEFAULT 0");
        }
    };

    private static final Migration MIGRATION_4_5 = new Migration(4, 5) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN startingWeight INTEGER NOT NULL DEFAULT -1");
        }
    };

    private static final Migration MIGRATION_5_6 = new Migration(5, 6) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE orderbean ADD COLUMN categoryId TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN categoryName TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN foodName TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN userId TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN mealStartTime TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN mealEndTime TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN mealTimeTypeCode TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN price TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN priceUnit TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN weightUnit TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN sign TEXT");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN mealTimeName TEXT");

            database.execSQL("ALTER TABLE orderbean ADD COLUMN calcType INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN onePortionWeight INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN floatingWeight INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN weight INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN whetherLimitWeightOfTake INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN limitWeightOfTake INTEGER NOT NULL DEFAULT 0");
            database.execSQL("ALTER TABLE orderbean ADD COLUMN startingWeight INTEGER NOT NULL DEFAULT -1");

            database.execSQL("ALTER TABLE mealinfo ADD COLUMN foodCategoryName TEXT");
            database.execSQL("ALTER TABLE mealinfo ADD COLUMN mealTimeTypeCode TEXT");

            database.execSQL("ALTER TABLE mealtimeinfo ADD COLUMN mealTimeTypeCode TEXT");

        }
    };

    private static final Migration MIGRATION_6_7 = new Migration(6, 7) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE orderbean ADD COLUMN clipWeight INTEGER NOT NULL DEFAULT 0");
        }
    };

}
